package com.ncmmall.api.apis.mall;

import com.ncmmall.api.apis.service.Result;
import com.ncmmall.domain.business.advertis.Advertis;
import com.ncmmall.querychannel.service.QueryChannelService;
import com.google.common.collect.ImmutableMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 广告api
 *
 * @author likaikai
 * @date 2019-07-17 11:11:27
 */
@RestController
public class AdvertisApi {

    @Autowired
    private QueryChannelService queryChannelService;

    private final Logger logger = LoggerFactory.getLogger(AdvertisApi.class);

    @Value("${store.domain}")
    private String storeDomain;

    /**
     * 广告
     */
    @GetMapping("/advertis")
    public Result advertis(int position) {
        Result result = new Result();
        try {
            Advertis advertis = queryChannelService.queryOneEntity("select * from mall_advertis where status = 2 and position = :position and platformType = 0", ImmutableMap.of("position", position), Advertis.class);
            if (advertis != null) {
                advertis.setImage(storeDomain + "/uploads/" + advertis.getImage());
            }
            return result.ok().inject(advertis);
        } catch (Exception e) {
            logger.error("[获取广告异常]", e);
            return result.fail().setMsg("网络请求异常");
        }
    }

}
